package com.antoco.operation.bigscreen.commons.validator;

import java.util.Optional;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.antoco.operation.bigscreen.commons.annotation.IsSensorExist;
import com.antoco.operation.bigscreen.model.entity.Sensor;
import com.antoco.operation.bigscreen.repository.SensorRepository;

import org.springframework.beans.factory.annotation.Autowired;

public class IsSensorExistValidator implements ConstraintValidator<IsSensorExist, Long> {

    @Autowired
    private SensorRepository sensorRepository;

    @Override
    public void initialize(IsSensorExist constraintAnnotation) {
    }

    @Override
    public boolean isValid(Long value, ConstraintValidatorContext context) {

        if (null == value) {
            return false;
        }else{
            Optional<Sensor> sensorOptional = sensorRepository.findById(value);
            if (sensorOptional.isPresent()) {
                return true;
            }else {
                return false;
            }
        }
    }
}
